﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Effects;
using Model.Modules;
using static Google.Protobuf.WellKnownTypes.Field.Types;


namespace Model
{
    public class TestEntitity
    {
        //int imgwidth = 1600, imgheight = 200;

        //说明每个平面内的相机号
        public Dictionary<int, int[]> surface_CamerasPairs = new Dictionary<int, int[]>();

        public string maind { get; set; }
        /// <summary>
        /// 创建一个模拟的批次出来，包括image，steel_defect,iamge_defect,全景
        /// </summary>
        /// <param name="batchid"></param>
        public TestEntitity(string batchid)
        {
            maind=batchid;
            //根据每个项目的相机和表面情况，定义此信息
            //4个表面，每个表面信息如下
            surface_CamerasPairs.Add(1, new int[] { 1 });
            surface_CamerasPairs.Add(2, new int[] { 2, 3 });
            surface_CamerasPairs.Add(3, new int[] { });
            surface_CamerasPairs.Add(4, new int[] { 4 });
            int imagetypecount = 3;
            int cameracount = 4;
            Imagelist = new List<image>();
            Random r = new Random();
            for (int i = 0; i < 2000; i++)
            {
                for (int g = 0; g < cameracount; g++)
                {
                    for (int imagetypeindex = 0; imagetypeindex < imagetypecount; imagetypeindex++)
                    {
                        try
                        {
                            var surface_id = surface_CamerasPairs.Where(t => t.Value.Contains(g + 1)).First().Key;
                        }
                        catch {
                        
                        }

                        Imagelist.Add(new image()
                        {
                            id = Guid.NewGuid().ToString(),
                            main_id = batchid,
                            flow_id = i,
                            steel_length = i * 0.125,
                            camera_id = g + 1,
                            surface_id = surface_CamerasPairs.Where(t => t.Value.Contains(g + 1)).First().Key,
                            image_url = Path.Combine(Environment.CurrentDirectory, imagetypeindex == 0 ? @"D:\BeijingProject\南南铝\pic\" + "1_" + r.Next(200, 210) + ".jpg" : @"D:\BeijingProject\南南铝\pic\light\" + "1_" + r.Next(200, 210) + ".jpg"),
                            width = 4096,
                            height = 2048,
                            type = imagetypeindex + 1,
                            insert_time = DateTime.Now,
                        }); ;
                    }
                }
            }

            Defectlist = new List<image_defect>();

            for (int i = 0; i < Imagelist.Count; i += imagetypecount)
            {
                var image = Imagelist[i];

                if (r.NextDouble() < 0.75)
                    continue;

                int defectcount = r.Next(2, 5);
                //int defectcount = 10;
                for (int c = 0; c < defectcount; c++)
                {
                    var x = r.Next(3500);
                    var y = r.Next(1800);
                    var w = r.Next(50, 400);
                    var h = r.Next(25, 400);
                    Defectlist.Add(new image_defect()
                    {
                        id = Guid.NewGuid().ToString(),
                        main_id = image.main_id,
                        x = x,
                        y = y,
                        w = w,
                        h = h,
                        crop_x = x - 20,
                        crop_y = y - 20,
                        crop_w = w + 40,
                        crop_h = h + 40,
                        real_x = x * 0.1,
                        real_y = y * 0.1 + i,
                        real_w = r.Next(50, 400) * 0.1,
                        real_h = r.Next(25, 400) * 0.1,
                        real_depth = r.Next(25, 400) * 0.1,
                        image_id = image.id,
                        image2_id = imagetypecount > 1 ? Imagelist[i + 1].id : "",
                        image3_id = imagetypecount > 2 ? Imagelist[i + 2].id : "",
                        camera_id = image.camera_id,
                        surface_id = image.surface_id,
                        type = (sbyte)r.Next(0, 30),
                        grade = r.Next(0, 5) == 4 ? "冗余存储" : r.Next(0, 4)==3 ? "报警" : r.Next(0, 3)==2?"警告": "疑似",
                        confidence = r.NextDouble(),
                        insert_time = image.insert_time,
                        is_visualize = true,
                        is_user_delete = false,

                    });
                }
            }
            Repeat_Defects = new List<repeat_defect>();
            foreach (var surface in surface_CamerasPairs)
            {
                if (surface.Value.Length == 0) continue;
                Repeat_Defects.Add(new repeat_defect
                {
                    id = Guid.NewGuid().ToString(),
                    main_id = batchid,
                    surface_id = surface.Key,
                    camera_id = surface.Value[0],
                    defect_type = r.Next(0, 10),
                    repeat_num = 0,
                    repeat_len = 1000,
                });
            }
            SteelDefectslist = new List<steel_defect>();
            for (int i = 0; i < Defectlist.Count; i++)
            {
                var defect = Defectlist[i];
                var rdm = r.Next(1,4);
                //if (rdm > 0.5)
                var steel_defect = new steel_defect()
                {
                    id = Guid.NewGuid().ToString(),
                    main_id = batchid,
                    //永钢项目为is_repeat为int，其他项目为bool
                    is_repeat = r.Next(2),
                    panorama_x = defect.x * 10,
                    panorama_y = defect.y * 10 + i * 100,
                    panorama_w = defect.w * 10,
                    panorama_h = defect.h * 10,
                    real_x = defect.x * 0.1,
                    real_y = defect.y * 0.1 + i,
                    real_w = defect.w * 0.1,
                    real_h = defect.h * 0.1,
                    real_depth = defect.real_depth,
                    confidence = defect.confidence,
                    type = defect.type,
                    surface_id = defect.surface_id,
                    grade = defect.grade,
                    insert_time = DateTime.Now.AddMinutes(i),
                    image_defect_ids_text = defect.id,
                };
                if (rdm > 0.9)
                {
                    var repeat = Repeat_Defects.Where(t => t.surface_id == defect.surface_id).First();
                    steel_defect.repeat_id = repeat.id;
                    repeat.repeat_num++;
                }
                SteelDefectslist.Add(steel_defect);

                defect.steel_defect_id = steel_defect.id;
                if (i + 1 < Defectlist.Count)
                {
                    Defectlist[i + 1].steel_defect_id = steel_defect.id;
                    i++;
                }

            }
            if (SteelDefectslist.Any())
            {
                SteelDefectslist.Last().id = "xxxxx";
                SteelDefectslist.Last().real_w = 10;
                SteelDefectslist.Last().real_h = 10;
                SteelDefectslist.Last().type = 1;
            }

            Batchlist = new List<batch>();
            for (int i = 0; i < 10; i++)
            {
                Batchlist.Add(new batch() { main_id = DateTime.Now.AddSeconds(-10 * i).ToString("yyyyMMddHHmmss"), insert_time = DateTime.Now.AddMinutes(-i) });
            }
        }


        public TestEntitity(DateTime start, DateTime end)
        {
            Imagelist = new List<image>();
            Random r = new Random();
            for (int i = 0; i < 100; i++)
            {
                for (int g = 0; g < 4; g++)
                {
                    Imagelist.Add(new image()
                    {
                        id = Guid.NewGuid().ToString(),
                        main_id = r.Next(100, 500).ToString(),
                        flow_id = i,

                        camera_id = g + 1,

                        image_url = Path.Combine(Environment.CurrentDirectory, @"Image\Resources\defectimg\" + "data_" + r.Next(1, 7) + "processed.bmp"),

                        type = r.Next(3)
                    });
                }
            }

            Defectlist = new List<image_defect>();
            foreach (var image in Imagelist)
            {

                if (r.NextDouble() < 0.1)
                    continue;
                var x = r.Next(800);
                var y = r.Next(800);
                Defectlist.Add(new image_defect()
                {
                    id = Guid.NewGuid().ToString(),
                    main_id = image.main_id,
                    x = x,
                    y = y,
                    w = r.Next(400),
                    h = r.Next(400),
                    image_id = image.id,
                    camera_id = image.camera_id,
                    type = (sbyte)r.Next(0, 18),

                });

            }
            SteelDefectslist = new List<steel_defect>();
            for (int i = 0; i < Defectlist.Count; i++)
            {
                var rdm = r.NextDouble();
                if (rdm > 0.5)
                {
                    SteelDefectslist.Add(new steel_defect()
                    {
                        id = Guid.NewGuid().ToString(),
                        main_id = r.Next(100, 500).ToString(),
                        type = (int)Defectlist[i].type,
                        //永钢项目为image_defect_ids_text，其他项目为image_defect_ids
                        image_defect_ids_text = Defectlist[i].id,
                        
                        is_repeat = r.Next(2),
                       
                        insert_time = DateTime.Now
                    });
                }
                else if (rdm > 0.2)
                {
                    steel_defect steel_Defect = new steel_defect()
                    {
                        id = Guid.NewGuid().ToString(),
                        main_id = r.Next(100, 500).ToString(),
                        type = (int)Defectlist[i].type,
                        image_defect_ids_text = "",
                       
                        is_repeat = r.Next(2),
                        
                        insert_time = DateTime.Now

                    };

                    for (int ii = 0; ii < 3; ii++)
                    {
                        if (ii == 0)
                        {
                            steel_Defect.image_defect_ids_text += Defectlist[i].id;
                        }
                        else
                        {
                            i++;
                            if (i >= Defectlist.Count) return;
                            steel_Defect.image_defect_ids_text += "," + Defectlist[i].id;
                        }
                    }
                    SteelDefectslist.Add(steel_Defect);
                }
                else
                {
                    steel_defect steel_Defect = new steel_defect()
                    {
                        id = Guid.NewGuid().ToString(),
                        main_id = r.Next(100, 500).ToString(),
                        type = (int)Defectlist[i].type,
                        image_defect_ids_text = "",
                       
                        is_repeat = r.Next(2),
                      
                        insert_time = DateTime.Now

                    };

                    for (int ii = 0; ii < 6; ii++)
                    {
                        if (ii == 0)
                        {
                            steel_Defect.image_defect_ids_text += Defectlist[i].id;
                        }
                        else
                        {
                            i++;
                            if (i >= Defectlist.Count) return;
                            steel_Defect.image_defect_ids_text += "," + Defectlist[i].id;
                        }
                    }
                    SteelDefectslist.Add(steel_Defect);
                }

            }


            Batchlist = new List<batch>();
            for (int i = 0; i < 10; i++)
            {
                Batchlist.Add(new batch() { main_id = DateTime.Now.AddSeconds(-10 * i).ToString("yyyyMMddHHmmss"), insert_time = DateTime.Now.AddMinutes(-i) });
            }
        }

        List<image> imagelist;
        List<image_defect> defectlist;
        List<steel_defect> sDefectslist;
        public List<image> Imagelist { get => imagelist; set => imagelist = value; }
        public List<image_defect> Defectlist { get => defectlist; set => defectlist = value; }
        public List<repeat_defect> Repeat_Defects { get; set; }
        public List<steel_defect> SteelDefectslist { get => sDefectslist; set => sDefectslist = value; }
        public List<batch> Batchlist { get; set; }
        Random r = new Random();
        public image getLastImage(int cameraid, int imagetype)
        {

            return new image()
            {
                id = Guid.NewGuid().ToString(),
                main_id = Guid.NewGuid().ToString(),
                flow_id = 0,
                camera_id = cameraid,
                surface_id = surface_CamerasPairs.Where(t => t.Value.Contains(cameraid)).First().Key,
                image_url = Path.Combine(Environment.CurrentDirectory, imagetype == 1 ? @"Image\Resources\defectimg\" + "data_" + r.Next(2, 7) + "processed.bmp" : @"Image\Resources\defectimg\deep\" + "data_" + r.Next(2, 7) + "processed.bmp"),
                type = imagetype,
                insert_time = DateTime.Now,
            };
        }
    }
}
